package me.zhengjie.modules.quartz.task;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.business.domain.Zhsq;
import me.zhengjie.business.domain.vo.PersonVo;
import me.zhengjie.business.repository.ZhsqRepository;
import me.zhengjie.business.service.PgjlService;
import me.zhengjie.business.service.dto.PgjlDto;
import me.zhengjie.business.service.dto.PgjlQueryCriteria;
import me.zhengjie.pdf.make.MakePdf;
import me.zhengjie.pdf.vo.PDFVO;
import me.zhengjie.utils.StringUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

/**
 * 生产评估结论
 * @author xiahongbo
 * @date 2023-10-19
 */
@Slf4j
@Async
@Component
public class PgjlTask {

    @Autowired
    private PgjlService pgjlService;

    @Transactional
    public void make1(String blurry) {
        // 当前时间

        PgjlQueryCriteria criteria = new PgjlQueryCriteria();
        String[] params = blurry.split("=");
        String p1 = params[0];
        String p2 = params[1];
        if("sfz".equals(p1)) {
            criteria.setSfz(p2);
        }
        if("pgrq".equals(p1)) {
            criteria.setPgrq(p2);
        }
        if("jgbm".equals(p1)) {
            criteria.setJgbm(p2);
        }

        String datetime1 = cn.hutool.core.date.DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_MS_PATTERN);
        List<PgjlDto> list = pgjlService.queryAll(criteria);
        for (int i = 0; i < list.size(); i++) {
            PgjlDto pgjlDto = list.get(i);
            MakePdf makePdf = new MakePdf();
            JSONObject freeLanceObject = new JSONObject();
            freeLanceObject.put("name", pgjlDto.getXm());
            freeLanceObject.put("idcard", pgjlDto.getSfz());
            freeLanceObject.put("applicant", pgjlDto.getSqr());
            freeLanceObject.put("assesslv", pgjlDto.getSndj());
            freeLanceObject.put("sftg", pgjlDto.getIsfh());
            freeLanceObject.put("assessdate", pgjlDto.getPgjl());
            freeLanceObject.put("C1_1", pgjlDto.getC01());
            freeLanceObject.put("C1_2", pgjlDto.getC02());
            freeLanceObject.put("C1_3", pgjlDto.getC03());
            freeLanceObject.put("C1_4", pgjlDto.getC04());
            freeLanceObject.put("C1_5", pgjlDto.getC05());
            freeLanceObject.put("C1_6", pgjlDto.getC06());
            freeLanceObject.put("C1_7", pgjlDto.getC07());
            freeLanceObject.put("C1_8", pgjlDto.getC08());
            freeLanceObject.put("C1_9", pgjlDto.getC09());
            freeLanceObject.put("C1_10", pgjlDto.getC10());
            freeLanceObject.put("C1", pgjlDto.getC1());
            freeLanceObject.put("C1_ASSESSOR_1", pgjlDto.getPgr());
            freeLanceObject.put("C1_ASSESSOR_2", "");
            freeLanceObject.put("C2_11", pgjlDto.getC11());
            freeLanceObject.put("C2_12", pgjlDto.getC12());
            freeLanceObject.put("C2_13", pgjlDto.getC13());
            freeLanceObject.put("C2_14", pgjlDto.getC14());
            freeLanceObject.put("C2", pgjlDto.getC2());
            freeLanceObject.put("C2_ASSESSOR_1", pgjlDto.getPgr());
            freeLanceObject.put("C2_ASSESSOR_2", "");
            freeLanceObject.put("C3_15", pgjlDto.getC15());
            freeLanceObject.put("C3_16", pgjlDto.getC16());
            freeLanceObject.put("C3_17", pgjlDto.getC17());
            freeLanceObject.put("C3", pgjlDto.getC3());
            freeLanceObject.put("C3_ASSESSOR_1", pgjlDto.getPgr());
            freeLanceObject.put("C3_ASSESSOR_2", "");

            List<PDFVO> list2 = makePdf.getCoordinate(freeLanceObject);

            String datetime = pgjlDto.getJgbm()+"-"+pgjlDto.getPgjl()+"-"+pgjlDto.getPgr()+"-"+pgjlDto.getXm()+"-"+pgjlDto.getSfz();
            String pdfName = datetime+".pdf";

            File fileDemo = new File("C:\\eladmin\\file\\评估结论书和评估表.pdf");//样例文件 固定地址存放
            File fileFont = new File("C:\\eladmin\\file\\songti.ttf");//字体 固定地址存放


            String folderPath = "D:\\WPSCloud\\"+pgjlDto.getJgbm();
            File folder = new File(folderPath);

            // 检查文件夹是否存在
            if (!folder.exists()) {
                // 创建文件夹
                folder.mkdirs(); // 使用mkdirs()确保父目录也被创建
            }


            File fileTarget = new File(folderPath+"\\" +pdfName);//目标文件 地址可以直接创建在临时目录
            try{
                Files.copy(fileDemo.toPath(), fileTarget.toPath());//样例文件内容 复制到 目标文件
                PDDocument template = PDDocument.load(fileTarget);//对目标文件进行操作
                //遍历添加文本
                for (PDFVO pdfvo : list2){
                    System.out.println(pdfvo.getName()+"----"+pdfvo.getValue());
                    PDPage page = template.getPage(pdfvo.getPage());
                    PDPageContentStream contentStream = new PDPageContentStream(template, page, PDPageContentStream.AppendMode.APPEND, false, true);
                    PDType0Font font = PDType0Font.load(template, fileFont);
                    // PDFont font = PDType1Font.HELVETICA_BOLD;
                    try {
                        contentStream.beginText();
                        contentStream.setFont(font, 14);
                        contentStream.newLineAtOffset(pdfvo.getTx(), pdfvo.getTy());
                        contentStream.showText(pdfvo.getValue());
                        contentStream.endText();
                    } finally {
                        contentStream.close();
                    }
                }
                //写入权限
                fileTarget.setWritable(true);
                // 保存PDF文件
                template.save(fileTarget);
                template.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    @Transactional
    public void make2() {
        // 当前时间
        PgjlQueryCriteria criteria = new PgjlQueryCriteria();
        String datetime1 = cn.hutool.core.date.DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_MS_PATTERN);
        List<PgjlDto> list = pgjlService.queryAll(criteria);
        for (int i = 0; i < list.size(); i++) {
            PgjlDto pgjlDto = list.get(i);
            System.out.println(i+"------------------"+pgjlDto.getXm());
            MakePdf makePdf = new MakePdf();
            JSONObject freeLanceObject = new JSONObject();
            freeLanceObject.put("name", pgjlDto.getXm());
            freeLanceObject.put("idcard", pgjlDto.getSfz());
            freeLanceObject.put("applicant", pgjlDto.getSqr());
            freeLanceObject.put("assesslv", pgjlDto.getSndj());
            freeLanceObject.put("sftg", pgjlDto.getIsfh());
            freeLanceObject.put("assessdate", pgjlDto.getPgjl());
            freeLanceObject.put("C1_1", pgjlDto.getC01());
            freeLanceObject.put("C1_2", pgjlDto.getC02());
            freeLanceObject.put("C1_3", pgjlDto.getC03());
            freeLanceObject.put("C1_4", pgjlDto.getC04());
            freeLanceObject.put("C1_5", pgjlDto.getC05());
            freeLanceObject.put("C1_6", pgjlDto.getC06());
            freeLanceObject.put("C1_7", pgjlDto.getC07());
            freeLanceObject.put("C1_8", pgjlDto.getC08());
            freeLanceObject.put("C1_9", pgjlDto.getC09());
            freeLanceObject.put("C1_10", pgjlDto.getC10());
            freeLanceObject.put("C1", pgjlDto.getC1());
            freeLanceObject.put("C1_ASSESSOR_1", pgjlDto.getPgr());
            freeLanceObject.put("C1_ASSESSOR_2", "");
            freeLanceObject.put("C2_11", pgjlDto.getC11());
            freeLanceObject.put("C2_12", pgjlDto.getC12());
            freeLanceObject.put("C2_13", pgjlDto.getC13());
            freeLanceObject.put("C2_14", pgjlDto.getC14());
            freeLanceObject.put("C2", pgjlDto.getC2());
            freeLanceObject.put("C2_ASSESSOR_1", pgjlDto.getPgr());
            freeLanceObject.put("C2_ASSESSOR_2", "");
            freeLanceObject.put("C3_15", pgjlDto.getC15());
            freeLanceObject.put("C3_16", pgjlDto.getC16());
            freeLanceObject.put("C3_17", pgjlDto.getC17());
            freeLanceObject.put("C3", pgjlDto.getC3());
            freeLanceObject.put("C3_ASSESSOR_1", pgjlDto.getPgr());
            freeLanceObject.put("C3_ASSESSOR_2", "");

            List<PDFVO> list2 = makePdf.getCoordinate(freeLanceObject);

            String datetime = pgjlDto.getJgbm()+"-"+pgjlDto.getPgjl()+"-"+pgjlDto.getPgr()+"-"+pgjlDto.getXm()+"-"+pgjlDto.getSfz();
            String pdfName = datetime+".pdf";

            File fileDemo = new File("C:\\eladmin\\file\\评估结论书和评估表.pdf");//样例文件 固定地址存放
            File fileFont = new File("C:\\eladmin\\file\\"+"songti.ttf");//字体 固定地址存放


            String folderPath = "D:\\WPSCloud\\"+pgjlDto.getPgrq();
            File folder = new File(folderPath);

            // 检查文件夹是否存在
            if (!folder.exists()) {
                // 创建文件夹
                folder.mkdirs(); // 使用mkdirs()确保父目录也被创建
            }


            File fileTarget = new File(folderPath+"\\" +pdfName);//目标文件 地址可以直接创建在临时目录
            try{
                Files.copy(fileDemo.toPath(), fileTarget.toPath());//样例文件内容 复制到 目标文件
                PDDocument template = PDDocument.load(fileTarget);//对目标文件进行操作
                //遍历添加文本
                for (PDFVO pdfvo : list2){
                    PDPage page = template.getPage(pdfvo.getPage());
                    PDPageContentStream contentStream = new PDPageContentStream(template, page, PDPageContentStream.AppendMode.APPEND, false, false);
                    // PDType0Font font = PDType0Font.load(template, fileFont);
                    PDFont font = PDType1Font.HELVETICA_BOLD;
                    contentStream.beginText();
                    contentStream.setFont(font, 14);
                    contentStream.newLineAtOffset(pdfvo.getTx(), pdfvo.getTy());//位置
                    contentStream.showText(pdfvo.getValue());//值
                    contentStream.endText();
                    contentStream.close();
                }
                //写入权限
                fileTarget.setWritable(true);
                // 保存PDF文件
                template.save(fileTarget);
                template.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
